import pymysql


class Tool(object):
    def __init__(self, user="root", password="123456", database="ATM", host="localhost", port=3306, charset="utf8",
                 autocommit=True):
        self.connection = pymysql.connect(host=host, user=user, password=password, database=database, charset=charset,
                                          port=port, autocommit=autocommit)

    def update(self, sql):
        self.cursor = self.connection.cursor()
        try:
            self.cursor.execute(sql)
        except:
            self.connection.rollback()
        finally:
            self.cursor.close()

    def select(self, sql):
        self.cursor = self.connection.cursor()
        try:
            self.cursor.execute(sql)
            result = self.cursor.fetchall()
            return result
        except:
            print("Error:查询数据库失败!")
        finally:
            self.cursor.close()
        del lst[i]

    def __del__(self):
        self.connection.close()
